package InterviewPractise.sort;

import java.sql.Array;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Richard
 * Date: 5/20/11
 * Time: 8:03 AM
 * To change this template use File | Settings | File Templates.
 */
public class SortCheck {
    public static void main(String[] args){
        //if we use the hash storage , then we don't use the sort method for sorting
        //else we need use the comparable or comparator
        Set<Person> persons = new HashSet<Person>();
        List<PersonInComparable> persons2 = new ArrayList<PersonInComparable>();
        List<Person> persons3 = new ArrayList<Person>();

        Random rand = new Random(4);

        for(int i =0 ; i<100; i++){
            persons.add(new Person("p"+i,rand.nextInt(100),"add" + i));
            persons2.add(new PersonInComparable("p"+i,rand.nextInt(),"add" + i));
        }
        //Collections.sort(persons);
        System.out.println("=========sort first time on the hashCode=======");
        for(Person p : persons){

           System.out.print(p.getName());
            System.out.println("-----"+p.getAge());
        }

        //how about if we reset the person age , will it update the hash table
        for(Person p : persons){
           p.setAge(rand.nextInt(100));
        }


        //no , it will not . and the hash set still order by its original order
        System.out.println("=============sort second time on the hashCode==========");
        for(Person p : persons){
           System.out.print(p.getName());
            System.out.println("-----"+p.getAge());
        }

        System.out.println("=======================================================");
        for(PersonInComparable p : persons2){
            System.out.print(p.getName());
            System.out.println("-----"+p.getAge());
        }


        System.out.println("=======================sorted================================");
        Collections.sort(persons2);
        for(PersonInComparable p : persons2){
            System.out.print(p.getName());
            System.out.println("-----"+p.getAge());
        }

        System.out.println("=======================sorted================================");
        Collections.sort(persons3,new PersonCompartor());
        for(PersonInComparable p : persons2){
            System.out.print(p.getName());
            System.out.println("-----"+p.getAge());
        }
    }
}
